package com.xhdx.es.adau.controller;

import java.util.Optional;

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xhdx.es.adau.dao.SysUserDao;
import com.xhdx.es.adau.entity.SysUser;
import com.xhdx.es.adau.util.BaseRespone;

@RestController
@RequestMapping("/elasticsh/sysuser")
public class SysUserController {
	@Autowired
    private SysUserDao sysUserDao;
	
    @RequestMapping("/str")
    public String list() {
        
        return "成功";
    }
    
    //添加
    @RequestMapping("/addUser")
    public SysUser addUser(@RequestBody SysUser userEntity) {
      return sysUserDao.save(userEntity);    
    } 
    
    //查询
    @RequestMapping("/findById")
    public Optional<SysUser> findById(String id) {
       return sysUserDao.findById(id);    
    }
    //多条件查询and
    @RequestMapping("/findSearchAnd")
    public BaseRespone<Iterable<SysUser>> findSearchAnd(String name, String mobile) {
    	if(StringUtils.isEmpty(name) && StringUtils.isEmpty(mobile)){
    		return BaseRespone.SUCCESS.content(sysUserDao.findAll());
    	}
    	return BaseRespone.SUCCESS.content(sysUserDao.findByNameAndMobile(name, mobile)); 
    }
   //多条件查询and
    @RequestMapping("/findSearchAndLoginNo")
    public BaseRespone<Iterable<SysUser>> findSearchAndLoginNo(String name, String loginNo) {
    	if(StringUtils.isEmpty(name) && StringUtils.isEmpty(loginNo)){
    		return BaseRespone.SUCCESS.content(sysUserDao.findAll());
    	}
    	return BaseRespone.SUCCESS.content(sysUserDao.findByNameAndLoginNo(name, loginNo)); 
    }
    //多条件查询or
    @RequestMapping("/findSearchOr")
    public BaseRespone<Iterable<SysUser>> findSearchOr(String name, String mobile) {
    	if(StringUtils.isEmpty(name) && StringUtils.isEmpty(mobile)){
    		return BaseRespone.SUCCESS.content(sysUserDao.findAll());
    	}
    	return BaseRespone.SUCCESS.content(sysUserDao.findByNameOrMobile(name, mobile)); 
    }
    /**
     * 使用QueryBuilder
     * termQuery("key", obj) 完全匹配
     * termsQuery("key", obj1, obj2..)   一次匹配多个值
     * matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
     * multiMatchQuery("text", "field1", "field2"..);  匹配多个字段, field有通配符忒行
     * matchAllQuery();         匹配所有文件
     */
    @RequestMapping("/queryBuilderSearch")
    public BaseRespone<Iterable<SysUser>> queryBuilderSearch(SysUser user) {
    	QueryBuilder query = QueryBuilders.matchQuery("name", user.getName());

    	return BaseRespone.SUCCESS.content(sysUserDao.search(query));
    }
    /**
     * 组合查询
     * must(QueryBuilders) :   AND
     * mustNot(QueryBuilders): NOT
     * should:                  : OR
     */
    @RequestMapping("/queryBuilderSearchZh")
    public BaseRespone<Iterable<SysUser>> queryBuilderSearchZh(SysUser user) {
    	QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("name", user.getName()))
                .mustNot(QueryBuilders.termQuery("email", user.getEmail()))
                .should(QueryBuilders.termQuery("login_no", user.getLoginNo()));

    	return BaseRespone.SUCCESS.content(sysUserDao.search(queryBuilder));
    }
}
